package com.young.aidl_base_server;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.young.aidl_base_sdk.ICalculator;
import com.young.aidl_base_sdk.ICalculatorListener;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "Young-AIDL-Server-" + MainActivity.class.getSimpleName();
    public static int num = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ApplicationInfo appInfo = getApplicationInfo();

        String processName = appInfo.processName;
        Log.e(TAG, "hello tiankon pid=" + processName);
        num = 30;
        initClick();
        initServer();
    }

    private void initClick() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.btn_do:
                        Random random = new Random();
                        for (ICalculatorListener value : CalculatorService.mListenerMap.values()) {
                            try {
                                value.onCallback("服务端主动发送消息给客户端: "+random.nextInt(101));
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                        CalculatorService.notifyClients();
                        try {
                            Log.d(TAG, "onClick: kk = "+mCalculator.add(1,1));
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };

        findViewById(R.id.btn_do).setOnClickListener(listener);
    }


    // AIDL 接口
    private ICalculator mCalculator;

    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mCalculator = ICalculator.Stub.asInterface(service);
            // 服务端的这里的 service 是否当前的 CalculatorService 对象，客户端的则是 BinderProxy
            Log.d(TAG, "onServiceConnected: mCalculator = "+mCalculator.hashCode()+" service = "+service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private void initServer(){


        Intent intent = new Intent();
        intent.setAction("com.young.CUSTOMER_SERVICE");
        intent.setComponent(new ComponentName("com.young.aidl_base_server",
                "com.young.aidl_base_server.CalculatorService"));
        boolean connected = bindService(intent, mServiceConnection, BIND_AUTO_CREATE);

//        Intent intent = new Intent(getApplicationContext(),CalculatorService.class);
//        intent.setAction("com.young.CUSTOMER_SERVICE");
//        bindService(intent,mServiceConnection,BIND_AUTO_CREATE);
    }

}